package com.yh.service.impl;

import com.yh.dao.ActorDao;
import com.yh.entity.Actor;
import com.yh.service.ActorService;
import com.yh.vo.ActorVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yuhuan
 * @date 2022/01/13
 */
@DubboService
public class ActorServiceImpl implements ActorService {
    @Autowired
    private ActorDao actorDao;

    @Override
    public List<ActorVO> findActorAll() {
        List<Actor> actorList = actorDao.findActorAll();
        List<ActorVO> voList = new ArrayList<>();
        if (ObjectUtils.isEmpty(actorList)) {
            return voList;
        }
        actorList.forEach(actor -> {
            ActorVO vo = new ActorVO();
            BeanUtils.copyProperties(actor, vo);
            voList.add(vo);
        });
        return voList;
    }

    @Override
    public int addActor(ActorVO vo) {
        Actor actor = new Actor();
        BeanUtils.copyProperties(vo, actor);
        // 去除名字两端的空格符
        String actorName = actor.getActorName().trim();
        actor.setActorName(actorName);
        return actorDao.insertActor(actor);
    }
}
